package com.xymtop.Controller;

import com.xymtop.Annotation.Controller;
import com.xymtop.Annotation.Mapping;
import com.xymtop.Annotation.Rest;
import com.xymtop.Entity.MyCourse;
import com.xymtop.Entity.Selection;
import com.xymtop.Entity.Times;
import com.xymtop.Server.ResoultJson;
import com.xymtop.Service.CourseService;
import com.xymtop.Server.Util.DBService;
import com.xymtop.Server.Util.Time;
import com.xymtop.Vo.CourseVo;
import com.xymtop.Vo.SelectCourse;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName : CourseController
 * @Description : 课程控制器
 * @Author : 肖叶茂
 * @Date: 2022/12/13  23:59
 */
@Controller(router = "course")
public class CourseController extends HttpServlet {

    private CourseService courseService = new CourseService();

    @Rest
    @Mapping(url = "getList")
    public ResoultJson<Object> getList(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
      String major = request.getParameter("major");
      if(major!=null) {
          List<Object> objects = courseService.getList(major);
          if (objects == null) {
              return new ResoultJson(204);
          } else {
              return new ResoultJson(0, objects, "");
          }
      }else {
          List<Object> objects = DBService.Get("SELECT * FROM `javaweb`.`courseview`",CourseVo.class.getCanonicalName());
          System.out.println("obj"+objects.toString());
          if(objects.isEmpty()){
              return new ResoultJson(204);
          }else {
              return new ResoultJson(0,objects,"");
          }
      }
    }


    @Rest
    @Mapping(url = "add")
    public ResoultJson<Object> add(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String name = request.getParameter("name");
        String teacher = request.getParameter("teacher");
        String time = request.getParameter("time");
        String address = request.getParameter("address");
        String major = request.getParameter("major");

        MyCourse myCourse = new MyCourse();

        myCourse.id = Time.getUnixTime();
        myCourse.name = name;
        myCourse.teacher = teacher;
        myCourse.address=address;
        myCourse.time = time;
        myCourse.code = "1";
        myCourse.major = major;



        if(DBService.InsertObject(myCourse,"course")){
            return new ResoultJson(200);
        }else {
            return new ResoultJson(204);
        }

    }

    @Rest
    @Mapping(url = "getListTea")
    public ResoultJson<Object> getListTea(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String userid = request.getParameter("userid");

            List<Object> objects = DBService.Get("SELECT * FROM `javaweb`.`courseview` where teacherid = "+userid ,CourseVo.class.getCanonicalName());
            if(objects.isEmpty()){
                return new ResoultJson(204);
            }else {
                return new ResoultJson(0,objects,"");
            }

    }


    @Rest
    @Mapping(url = "select")
    public ResoultJson<String> select(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException {
        String stuid = request.getParameter("stuid");
        String courseid = request.getParameter("courseid");


        Selection selection = new Selection();
        selection.time =Time.getTimeByUnix( Time.getUnixTime());
        selection.id = Time.getUnixTime();
        selection.courseid =courseid;
        selection.stuid =stuid;

        Boolean flag = DBService.InsertObject(selection,"seletion");

        if(flag){
            return  new ResoultJson(200);
        }else {
            return new ResoultJson(204);
        }
    }

    @Rest
    @Mapping(url = "isSelect")
    public ResoultJson<String> isSelect(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        String stuid = request.getParameter("stuid");
        String courseid = request.getParameter("courseid");

        List<Object> objects  =   DBService.Get("select * from seletion where courseid = "+courseid+" and stuid = "+stuid, Selection.class.getCanonicalName());
        if(objects.isEmpty()){
            return  new ResoultJson(204);
        }else {
            return  new ResoultJson(200);
        }
    }


    @Rest
    @Mapping(url = "delSelect")
    public ResoultJson<String> delSelect(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        String stuid = request.getParameter("stuid");
        String courseid = request.getParameter("courseid");

        Selection selection = new Selection();
        selection.courseid =courseid;
        selection.stuid = stuid;
      int flag = DBService.execSql("DELETE FROM `seletion` WHERE stuid = "+stuid+" and courseid = "+courseid+"; ");
        if(flag!=0){
            return  new ResoultJson(200);
        }else {
            return  new ResoultJson(204);
        }
    }

    @Rest
    @Mapping(url = "timetable")
    public  ResoultJson<Object> timeTable(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String stuid = request.getParameter("stuid");
        List<Object> objects =   DBService.Get("SELECT selected.* FROM selected WHERE selected.stuid = "+stuid, SelectCourse.class.getCanonicalName());
        if(objects.isEmpty()){
            return new ResoultJson(204);
        }else {
            return  new ResoultJson(200,objects,"");
        }
    }

    @Rest
    @Mapping(url = "timeslist")
    public  ResoultJson<Object> timeList(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "SELECT * FROM `times`";
        List<Object> objects = DBService.Get(sql,Times.class.getCanonicalName());
        return new ResoultJson<>(0,objects,"");
    }

    @Rest
    @Mapping(url = "addtimes")
    public  ResoultJson<Object> addtimes(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException {
             Times times = new Times();
             times.id = Time.getUnixTime();
             times.code = "1";
             times.content = request.getParameter("content");
             times.detail = request.getParameter("detail");
             if(DBService.InsertObject(times,"times")){
                 return  new ResoultJson<>(200);
             }else {
                 return  new ResoultJson<>(204);
             }
    }

    @Rest
    @Mapping(url = "deltimes")
    public  ResoultJson<Object> deltimes(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException {
        if(DBService.DelObjectById(request.getParameter("id"),"times")){
            return  new ResoultJson<>(200);
        }else {
            return  new ResoultJson<>(204);
        }
    }


}
